import apis from '@/apis/modules/base'
import { generatorDynamicRouter } from '@/router/generate-routers'

export default {
    state: {
        asyncRoutes: [],
    },
    mutations: {
        setRouters(state, routers) {
            state.asyncRoutes = routers
        }
    },
    actions: {
        //调用后台接口得到可访问菜单列表,生成路由
        generateRoutes({ commit }) {
            return new Promise((resolve, reject) => {
                apis.getSystemMenu().then(res => {
                    generatorDynamicRouter(res.data).then(routers => {
                        commit('setRouters', routers)
                        resolve()
                    }).catch(error => reject(error))
                }).catch(error => reject(error))
            })
        },

    }
}